﻿' <summary>
' BeeWit IDE Entorno de desarrollo Visual para php.
' BeeWitPHP IDE es el único entorno de desarrollo para PHP 
' de tipo WYSIWYG (What You See Is What You Get). 
' ¡Ahora si!, aproveche la potencia del lenguaje PHP 
' y desarrolle más rápidamente gracias a la programación 
' intuitiva de BeeWit.
' Copyright © 2010 Dewins Murillo Garcia 
' EMAIL: dewinsmg@gmail.com
' EMAIL: dewins@beewitsoft.com
' Este Programa es Software Libre: usted puede redistribuirlo 
' y/o modificarlo bajo los términos de la Licencia Publica General 
' GNU como es publicada por la Fundacion de Software Libre; 
' en la 3ra versión de la licencia.
' Este programa es distribuido con la esperanza de que sea útil, 
' pero SIN GARANTÍA ALGUNA; sin siquiera la garantía implícita 
' de VALOR COMERCIAL o FORMADO PARA UN PROPÓSITO EN PARTICULAR. 
' Vea la Licencia Publica General GNU para mas detalles.
' Usted debe haber recibido una copia de la Licencia Publica General 
' GNU junto con este programa. Si no, vaya a http://www.beewitsoft.com
' o en http://code.google.com/p/ajax-beewit-php-ide/
' </summary>
' <remarks></remarks>

Public Class ctlProperties
    Private Element As HtmlElement
    Dim id As String = ""

    Public Sub LoadAttributes(ByRef TargetImage As HtmlElement)
        'consultar el atributo
        Element = TargetImage
        Dim ClassName As String = ""
        If Element.TagName = "BODY" Then
            Exit Sub
        End If
        Dim CodigoClase As String = Element.GetAttribute("class_code")
        id = TargetImage.GetAttribute("id")
        Dim PHPCode As String = ""

        If CodigoClase <> "" Then
            ClassName = CodigoClase.Substring((CodigoClase.LastIndexOf("\")) + 1).Replace(".cjx", "")
        End If

        Dim NodeFound As clsNode = Control_Explorer.SearchClass(ClassName, Nothing)

        If Not NodeFound Is Nothing Then
            PHPCode = NodeFound.ContentCode
        Else
            If Not String.IsNullOrEmpty(CodigoClase) Then
                Control_Console.AddMessages("[" & CodigoClase & "] no found!!.  ", ctlConsole.enumTypeMessages.isWARNING)
            End If
        End If

        Dim objWords As New clsEditorCode
        Dim ListedVars(0) As clsRegExpr.Vars

        Dim i As Long = 0
        Dim bag1 As New PropertyBag
        AddHandler bag1.GetValue, AddressOf bag1_GetValue
        AddHandler bag1.SetValue, AddressOf bag1_SetValue

        objWords.Node = NodeFound

        Dim ListedCount As Long = 0

        ListedCount = objWords.ListProperties(PHPCode, clsEditorCode.Ambit.isPROTECTED, "", ListedVars)

        For i = 0 To ListedCount - 1
            Dim AttributeValue As String = Element.GetAttribute(ListedVars(i).VarName)
            Dim ObjProperty As PropertySpec = New PropertySpec(ListedVars(i).VarName, "System.String", "property", ListedVars(i).VarComment, "")
            bag1.Properties.Add(ObjProperty)
        Next
        '*******
        Dim AutoListCount As Long = 0
        Dim ClassNameInherit As String = ""
        ClassNameInherit = Object_Expression.GetClassNameInherits(PHPCode)
        NodeFound = Control_Explorer.SearchClass(ClassNameInherit)
        If Not NodeFound Is Nothing Then
            ListedCount = objWords.ListProperties(NodeFound.ContentCode, clsEditorCode.Ambit.isPROTECTED, "", ListedVars)
            For i = 0 To ListedCount - 1
                Dim AttributeValue As String = Element.GetAttribute(ListedVars(i).VarName)
                Dim ObjProperty As PropertySpec = New PropertySpec(ListedVars(i).VarName, "System.String", "property", ListedVars(i).VarComment, "")
                bag1.Properties.Add(ObjProperty)
            Next
        End If
        '******* TAGS PROPERTIES
        Dim TagsProperties() As clsTags.eProperties = Object_Tags.GetProperties(TargetImage.TagName)
        If Not TagsProperties Is Nothing Then
            For Each aTag As clsTags.eProperties In TagsProperties
                Dim AttributeValue As String = Element.GetAttribute(aTag.propertyname)
                Dim ObjProperty As PropertySpec = New PropertySpec(aTag.propertyname, "System.String", "property", aTag.description, "")
                bag1.Properties.Add(ObjProperty)
            Next
        End If
        '*******

        Dim listView As New ListView
        Dim item1 As ListViewItem = New ListViewItem(ClassName, 0)
        item1.Tag = bag1
        listView.Items.Add(item1)

        Dim objs As ArrayList = New ArrayList()
        For Each item As ListViewItem In listView.Items
            objs.Add(item.Tag)
        Next
        _propertyGrid.SelectedObjects = objs.ToArray

    End Sub

    Private Sub bag1_GetValue(ByVal sender As Object, ByVal e As PropertySpecEventArgs)
        Try
            e.Value = Element.GetAttribute(e.Property.Name)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub bag1_SetValue(ByVal sender As Object, ByVal e As PropertySpecEventArgs)
        Try
            Dim ChangeVarName As Boolean = False
            Dim OldPropertyValue As String = Element.GetAttribute(e.Property.Name)
            Dim ClassCode As String = Element.GetAttribute("class_code")

            Element.SetAttribute(e.Property.Name, e.Value)
            If e.Property.Name.ToUpper = "ID" Then
                Element.SetAttribute("NAME", e.Value)
                ChangeVarName = True
            End If
            If e.Property.Name.ToUpper = "NAME" Then
                Element.SetAttribute("ID", e.Value)
                ChangeVarName = True
            End If
            If e.Property.Name.ToUpper = "DISABLED" Then
                If e.Value.ToString.ToUpper = "FALSE" Then
                    Element.SetAttribute(e.Property.Name, Nothing)
                End If
            End If
            If ChangeVarName Then
                General.Control_Main.ActualEditor.Node.ContentCode = General.Control_Main.ActualEditor.Node.ContentCode.Replace("$" & OldPropertyValue, "$" & e.Value)
                General.Control_Main.ActualEditor.Node.ContentCode = General.Control_Main.ActualEditor.Node.ContentCode.Replace("->" & OldPropertyValue, "->" & e.Value)
                General.Control_Main.ActualEditor.Node.ContentCode = General.Control_Main.ActualEditor.Node.ContentCode.Replace("_" & OldPropertyValue & "_", "_" & e.Value & "_")
                General.Control_Main.ActualEditor._EditorControlCode.Document.Text = General.Control_Main.ActualEditor.Node.ContentCode
            End If
            'Dim ArchivoTmp As String = General.Control_Main.ActualEditor.CreateHtmlView(Element.OuterHtml, Element.Id, "", Element.GetAttribute("class_code"))
            Dim ArchivoTmp As String = General.Control_Main.ActualEditor.CreateHtmlView(Element.OuterHtml, Element.Id, Element.GetAttribute("class_code"))
            Dim Random As New Random()
            Dim numero As Integer = Random.Next(1, 1000000)
            Element.SetAttribute("src", Element.GetAttribute("src") & "?RANDOM=" & numero)
            General.Control_Main.ActualEditor.FlushHTML()
            'Element.DomElement.ExecCommand("Refresh", False, Nothing)
            Element.Document.ExecCommand("Refresh", False, Nothing)
            'General.Control_Main.ActualEditor.webDesign.Refresh()
            'Me.webDesign.Navigate(ArchivoTmp)
        Catch ex As Exception

        End Try
    End Sub

    Private Function GetFunctionClose(ByVal Content As String, ByVal PosStart As Long) As Long
        Dim CountOpen As Long = 0
        GetFunctionClose = PosStart
        For i = PosStart To Content.Length - 1
            GetFunctionClose = i
            If Mid(Content, i, 1) = "{" Then
                CountOpen += 1
            End If
            If Mid(Content, i, 1) = "}" Then
                CountOpen -= 1
                If CountOpen = 0 Then
                    Exit Function
                End If
            End If
        Next
    End Function

    Private Sub _propertyGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _propertyGrid.Click

    End Sub
End Class
